<?php 
class accountController
{
	public function register()
	{
		if( !$_POST['email'] )
		{
			Kernel::layer('view')->sub('register');
			return false;
		}
		
		if( !eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$" , $_POST['email']))
		{
			Kernel::layer('view')->render( Kernel::layer('lang')->notcorrectmail );
			Kernel::layer('view')->sub('register');
			return false;
		}
		
		$Account = new OTS_Account;
		$Account->find( $_POST['email'] );
		if( $Account->isLoaded() )
		{
			Kernel::layer('view')->render( Kernel::layer('lang')->emailarleadyexists; );
			Kernel::layer('view')->sub('register');
			return false;
		}
		
		$Password = substr( sha1( lcg_value() ), 0, rand(5, 7) );
		$Account->create( 111111, 9999999 );
		$Account->password = Kernel::layer('OTS')->password( $Password );
		$Account->email = $_POST['email'];
		$Account->blocked = 0;
		$Account->save();
		
		if( Kernel::layer('mail')->send( $Account,  'Account register', "Account:\n" . $Account->id . "\nPassword:\n" . $pass) )
		{
			Kernel::layer('view')->render(Kernel::layer('lang')->succesfullycreatedwm;);
		}
		else
		{
			Kernel::layer('view')->render( sprintf( Kernel::layer('lang')->succesfullycreate, $Account->id, $Password ) )
		}
	}
	
	public function index()
	{
		if( !Kernel::layer('session')->getAccount() )
		{
			Kernel::layer('controller')->load('login');
			return false;
		}
		
		$Account = new OTS_Account;
		$Account->load( Kernel::layer('session')->getAccount() );
		
		if( !$Account )
		{
			Kernel::layer('view')->render('ERROR');
			return false;
		}
		
		if( Kernel::layer('OTS')->type == 'tfs' )
		{
			$P = ( $Account->getCustomField('premdays') > 0 ) ? true : false;
		}
		else
		{
			$P = ( $Account->getCustomField('premend') > time() ) ? true : false;
		}
		
		Kernel::layer('view')->set('Premmy', $P);
		Kernel::layer('view')->set('Account', $Account);
		Kernel::layer('view')->sub('account');
	}
	
	public function password()
	{
		if( $_POST['password'] )
		{
			if( $_POST['newPassword'] == $_POST['confirmNewPassword'] )
			{
					$Account = new OTS_Account;
					$Account->load( Kernel::layer('session')->getAccount() );
		
					if( !$Account )
					{
						Kernel::layer('view')->render('ERROR');
						Kernel::layer('view')->sub('passwordchange');
						return false;
					}
					
					if( Kernel::layer('OTS')->password( $_POST['password'] ) == $Account->password )
					{
						Kernel::layer('session')->setPassword( Kernel::layer('OTS')->password( $_POST['confirmNewPassword'] ) );
						$Account->password = Kernel::layer('OTS')->password( $_POST['confirmNewPassword'] );
						$Account->save();
						Kernel::layer('view')->render( Kernel::layer('lang')->passwordchanged );
						return true;
						
					}
					else
					{
						Kernel::layer('view')->render( Kernel::layer('lang')->badpassword );
						Kernel::layer('view')->sub('passwordchange');
						return false;
					}
			}
			else
			{
				Kernel::layer('view')->render( Kernel::layer('lang')->haventrepeatedpassword );
				Kernel::layer('view')->sub('passwordchange');
				return false;
			}
		}
		
		Kernel::layer('view')->sub('passwordchange');
	}
}
?>